

rm(list = ls())

library(survey)
library(foreign)
library(doBy)
library(xtable)
library(ggplot2)
source(file="multiplot.R")

ccap <- read.dta("CCAP_idealpointsb.dta")
ccap$abb<-ccap$state

primary_type_dems <- read.csv("Primary Dates and Types 2008-2014 - Dems.csv",colClasses="character")
primary_type_reps <- read.csv("Primary Dates and Types 2008-2014 - Reps.csv",colClasses="character")

# recode dates as days from January 1
the_dates_dems <- as.Date(strptime(primary_type_dems$X2008.President.Date,format="%m/%d/%y"))
primary_type_dems$dem_days <- as.numeric(the_dates_dems - as.Date("2008-01-01"))

the_dates_reps <- as.Date(strptime(primary_type_reps$X2008.President.Date,format="%m/%d/%y"))
primary_type_reps$rep_days <- as.numeric(the_dates_reps - as.Date("2008-01-01"))

primary_type<-as.data.frame(cbind(primary_type_dems$abb, primary_type_dems$dem_days, primary_type_reps$rep_days, primary_type_dems$X2008.President.Type, primary_type_reps$X2008.President.Type))

colnames(primary_type)<-c("abb", "dem_days", "rep_days","dem_type", "rep_type")
primary_type$abb<-as.vector(primary_type$abb)
primary_type$dem_days<-as.numeric(as.vector(primary_type$dem_days))
primary_type$rep_days<-as.numeric(as.vector(primary_type$rep_days))

## self-report of which party they voted for in general
ccap$presgeneral_party<-NA
ccap$presgeneral_party[ccap$pcap600=="I voted for Barack Obama"] <-"Democrat"
ccap$presgeneral_party[ccap$pcap600=="I voted for John McCain"] <-"Republican"


## self-report of which party they voted for in primary
ccap$presprimary_party<-NA
ccap$presprimary_party[ccap$scap4=="Democrat"] <-"Democrat"
table(ccap$presprimary_party)
ccap$presprimary_party[(is.na(ccap$scap4) | ccap$scap4=="Not Asked") & ccap$mcap4_aux=="Democrat"] <-"Democrat"
table(ccap$presprimary_party)

ccap$presprimary_party[ccap$scap4=="Republican"] <-"Republican"
table(ccap$presprimary_party)
ccap$presprimary_party[(is.na(ccap$scap4) | ccap$scap4=="Not Asked") & ccap$mcap4_aux=="Republican"] <-"Republican"
table(ccap$presprimary_party)

ccap08 <- ccap

# merge the data
ccap08 <- cbind(primary_type[match(ccap$abb,primary_type$abb),],
              ccap)

# scap4 = retrospective primary choice (september)
# bcap4 = prospective primary choice (baseline)
# different primary dates...
# p = post, j = january, m = march, o=october
# e2008p = validated primaryturnout & registration

ccap08$primaryturnout <- NA

# first, use known primary primaryturnout and self reports of *which* primary
ccap08$primaryturnout[ccap08$presprimary_party=="Republican" & ccap08$e2008p != "NA"] <- "Republican"
ccap08$primaryturnout[ccap08$presprimary_party=="Democrat" & ccap08$e2008p != "NA"] <- "Democrat"
table(ccap08$primaryturnout)
# where we know from the voter file which primary, overwrite
ccap08$primaryturnout[ccap08$e2008p == "R"] <- "Republican"
ccap08$primaryturnout[ccap08$e2008p == "D"] <- "Democrat"
 table(ccap08$primaryturnout)
# for Iowa and Nevada, us self-reported primaryturnout in september
# (for some reason, earlier versions of these questions are mostly
# not asked)
ccap08$primaryturnout[ccap08$presprimary_party == "Republican" & ccap08$rep_type=="Caucus"] <- "Republican"
ccap08$primaryturnout[ccap08$presprimary_party == "Democrat" & ccap08$rep_type=="Caucus"] <- "Democrat"
table(ccap08$primaryturnout)

ccap08$pid7<-as.numeric(as.factor(ccap08$pcap8))
ccap08$pid7[ccap08$pid7>7]<-NA

##bprofile19, CIVIL UNIONS FOR GAY COUPLES
ccap08$bprofile19_binary[ccap08$bprofile19=="Strongly favor" | ccap08$bprofile19=="Somewhat favor"]<-1
ccap08$bprofile19_binary[ccap08$bprofile19=="Somewhat oppose" | ccap08$bprofile19=="Strongly oppose" | ccap08$bprofile19=="I'm not sure, I haven't thought much about this"]<-0
(table(ccap08$bprofile19, ccap08$bprofile19_binary))

##bcap24, TAXES 
ccap08$bcap24_binary[ccap08$bcap24=="St. Favor" | ccap08$bcap24=="Favor"]<-1
ccap08$bcap24_binary[ccap08$bcap24=="Oppose" | ccap08$bcap24=="St. Oppose"]<-0
(table(ccap08$bcap24, ccap08$bcap24_binary))

## recode variables
ccap08$age_exact<-2008-as.numeric(as.vector(ccap08$profile51))  
ccap08$idealpoint<-as.numeric(as.vector(ccap08$est))
ccap08$self_ideology<-as.numeric(as.factor(ccap08$scap700s))
ccap08$self_ideology[ccap08$self_ideology>5]<-NA

ccap08$discussed_candidate_oct[ccap08$ocap400_6=="Yes"]<-1
ccap08$discussed_candidate_oct[ccap08$ocap400_6=="No"]<-0
ccap08$political_interest2<- as.numeric(as.factor(ccap08$bcap813))
ccap08$political_interest2[ccap08$political_interest2>3]<-NA

ccap08$hs[ccap08$profile57=="High school graduate"]<-1
ccap08$hs[ccap08$profile57!="High school graduate"]<-0

ccap08$pol_interest[ccap08$political_interest2==1]<-1
ccap08$pol_interest[ccap08$political_interest2!=1]<-0

ccap08$college[ccap08$profile57=="College graduate" |ccap08$profile57== "Post-grad"]<-1
ccap08$college[is.na(ccap08$college)]<-0

ccap08$white[ccap08$profile55=="White"]<-1
ccap08$white[ccap08$profile55!="White"]<-0

ccap08<-subset(ccap08, !is.na(psweight) )
ccap08$age<-2012-as.numeric(as.vector(ccap08$age_exact))

ccap08$primaryturnout[is.na(ccap08$primaryturnout)]<-0

ccap08$voted_primary_validated[ccap08$primaryturnout!="0"]<-1
ccap08$voted_primary_validated[ccap08$primaryturnout=="0"]<-0

ccap08$general_noprimary<-NA
ccap08$general_noprimary[ccap08$e2008g!="NA" & ccap08$primaryturnout==0]<-1
ccap08$general_noprimary[is.na(ccap08$general_noprimary)]<-0

### Make a binary variable for whether people turned out in general 
ccap08$turnout_general_all<-NA
ccap08$turnout_general_all[ccap08$e2008g!="NA"]<-1
ccap08$turnout_general_all[is.na(ccap08$turnout_general_all)]<-0

### Make a binary variable for whether people turned out in general 
ccap08$turnout_primary_all<-NA
ccap08$turnout_primary_all[ccap08$e2008p!="NA"]<-1
ccap08$turnout_primary_all[is.na(ccap08$turnout_primary_all)]<-0

survey08<-ccap08
survey08$psweight<-as.numeric(as.vector(survey08$psweight))
survey08$weight<-as.numeric(as.vector(survey08$weight))

survey08.design<-svydesign(ids=survey08$caseid, data = survey08, weights=survey08$psweight)

survey08_primary<-subset(survey08,primaryturnout!="0" )
survey08_primary.design<-svydesign(ids=survey08_primary$caseid, data = survey08_primary, weights=survey08_primary$psweight)

survey08_general<-subset(survey08,survey08$e2008g!="NA"  )
survey08_general.design<-svydesign(ids=survey08_general$caseid, data = survey08_general, weights=survey08_general$psweight)

survey08_general_noprim<-subset(survey08,(survey08$e2008g!="NA" & primaryturnout=="0") )
survey08_general_noprim.design<-svydesign(ids=survey08_general_noprim$caseid, data = survey08_general_noprim, weights=survey08_general_noprim$psweight)



## cross-tabs of who voted
# by party

survey08$voted_primary_validated<-as.numeric(as.vector(survey08$voted_primary_validated))
survey08$general_noprimary<-as.numeric(as.vector(survey08$general_noprimary))
survey08$voted_primary_validated<-as.numeric(as.vector(survey08$voted_primary_validated))
survey08$general_noprimary<-as.numeric(as.vector(survey08$general_noprimary))

## make density graph of ideal points
mean_idealpoint_primary_dems <- svymean(survey08_primary$idealpoint[survey08_primary$primaryturnout=="Democrat"], design=survey08_primary.design[survey08_primary$primaryturnout=="Democrat"], na.rm=T)

mean_idealpoint_partyfollowing_dems <- svymean(survey08_general$idealpoint[survey08_general$presgeneral_party=="Democrat" | survey08_general$pid7<4], design=survey08_general.design[survey08_general$presgeneral_party=="Democrat" | survey08_general$pid7<4], na.rm=T)

mean_idealpoint_primary_reps <- svymean(survey08_primary$idealpoint[survey08_primary$primaryturnout=="Republican"], design=survey08_primary.design[survey08_primary$primaryturnout=="Republican"], na.rm=T)

mean_idealpoint_partyfollowing_reps <- svymean(survey08_general$idealpoint[survey08_general$presgeneral_party=="Republican" | survey08_general$pid7>4], design=survey08_general.design[survey08_general$presgeneral_party=="Republican" | survey08_general$pid7>4], na.rm=T)
    
survey08$Party[survey08$pid7<4]<-1
survey08$Party[survey08$pid7>4]<-3
survey08$Party<-as.character(survey08$Party)

###
### GRAPH FOR FIRST PANEL OF FIGURE 1
###

pdf('density_ccap08_presBW.pdf', height = 4, width = 7)
ggplot(subset(survey08, !is.na(Party)), aes(idealpoint, fill = Party)) +
geom_density(alpha = 0.1)+ scale_fill_manual(breaks = c("1", "3"),
                        values=c("grey",  "black"),labels=c("Democrats", "Republicans"),guide = FALSE)+
    geom_vline(xintercept = mean_idealpoint_primary_dems[1], linetype="longdash")+
     geom_vline(xintercept = mean_idealpoint_partyfollowing_dems[1])+
    geom_vline(xintercept = mean_idealpoint_primary_reps[1], linetype="longdash")+
    geom_vline(xintercept = mean_idealpoint_partyfollowing_reps[1])+
      xlab("Ideal Point") +
       annotate("text",label="Democrats",x=-1.2,y=.3,size=4)+
       annotate("text",label="Republicans",x=1.2,y=.3,size=4)+
       theme_bw() 
dev.off()

     
###
### CODE BELOW PRODUCES ROWS FOR CCAP PORTION OF TABLE 1
###

fulltable <- NULL

## age
col1<-svyquantile(~survey08_primary$age_exact[survey08_primary$primaryturnout=="Democrat"], design=survey08_primary.design[survey08_primary$primaryturnout=="Democrat"], na.rm=T, quantile=.5)
col2<-svyquantile(~survey08_general_noprim$age_exact[survey08_general_noprim$presgeneral_party=="Democrat"], design=survey08_general_noprim.design[survey08_general_noprim$presgeneral_party=="Democrat"], na.rm=T, quantile=.5)
col3<-svyquantile(~survey08_general$age_exact[survey08_general$presgeneral_party=="Democrat" | survey08_general$pid7<4], design=survey08_general.design[survey08_general$presgeneral_party=="Democrat" | survey08_general$pid7<4], na.rm=T, quantile=.5)

col4<-svyquantile(~survey08_primary$age_exact[survey08_primary$primaryturnout=="Republican"], design=survey08_primary.design[survey08_primary$primaryturnout=="Republican"], na.rm=T, quantile=.5)
col5<-svyquantile(~survey08_general_noprim$age_exact[survey08_general_noprim$presgeneral_party=="Republican"], design=survey08_general_noprim.design[survey08_general_noprim$presgeneral_party=="Republican"], na.rm=T, quantile=.5)
col6<-svyquantile(~survey08_general$age_exact[survey08_general$presgeneral_party=="Republican" | survey08_general$pid7>4], design=survey08_general.design[survey08_general$presgeneral_party=="Republican" | survey08_general$pid7>4], na.rm=T, quantile=.5)
diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "age"; fulltable <- rbind(fulltable,mat)

## college degree
col1<-svymean(survey08_primary$college[survey08_primary$primaryturnout=="Democrat"], design=survey08_primary.design[survey08_primary$primaryturnout=="Democrat"], na.rm=T)
col2<-svymean(survey08_general_noprim$college[survey08_general_noprim$presgeneral_party=="Democrat"], design=survey08_general_noprim.design[survey08_general_noprim$presgeneral_party=="Democrat"], na.rm=T)
col3<-svymean(survey08_general$college[survey08_general$presgeneral_party=="Democrat" | survey08_general$pid7<4], design=survey08_general.design[survey08_general$presgeneral_party=="Democrat" | survey08_general$pid7<4], na.rm=T)

col4<-svymean(survey08_primary$college[survey08_primary$primaryturnout=="Republican"], design=survey08_primary.design[survey08_primary$primaryturnout=="Republican"], na.rm=T)
col5<-svymean(survey08_general_noprim$college[survey08_general_noprim$presgeneral_party=="Republican"], design=survey08_general_noprim.design[survey08_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6<-svymean(survey08_general$college[survey08_general$presgeneral_party=="Republican" | survey08_general$pid7>4], design=survey08_general.design[survey08_general$presgeneral_party=="Republican" | survey08_general$pid7>4], na.rm=T)

diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

col1<-paste(round(col1*100,0), "%",sep="")
col2<-paste(round(col2*100,0), "%",sep="")
col3<-paste(round(col3*100,0), "%",sep="")
col4<-paste(round(col4*100,0), "%",sep="")
col5<-paste(round(col5*100,0), "%",sep="")
col6<-paste(round(col6*100,0), "%",sep="")
diff_a<-paste(round(diff_a*100,0), "%",sep="")
diff_b<-paste(round(diff_b*100,0), "%",sep="")

xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "college"; fulltable <- rbind(fulltable,mat)

## white
col1<-svymean(survey08_primary$white[survey08_primary$primaryturnout=="Democrat"], design=survey08_primary.design[survey08_primary$primaryturnout=="Democrat"], na.rm=T)
col2<-svymean(survey08_general_noprim$white[survey08_general_noprim$presgeneral_party=="Democrat"], design=survey08_general_noprim.design[survey08_general_noprim$presgeneral_party=="Democrat"], na.rm=T)
col3<-svymean(survey08_general$white[survey08_general$presgeneral_party=="Democrat" | survey08_general$pid7<4], design=survey08_general.design[survey08_general$presgeneral_party=="Democrat" | survey08_general$pid7<4], na.rm=T)

col4<-svymean(survey08_primary$white[survey08_primary$primaryturnout=="Republican"], design=survey08_primary.design[survey08_primary$primaryturnout=="Republican"], na.rm=T)
col5<-svymean(survey08_general_noprim$white[survey08_general_noprim$presgeneral_party=="Republican"], design=survey08_general_noprim.design[survey08_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6<-svymean(survey08_general$white[survey08_general$presgeneral_party=="Republican" | survey08_general$pid7>4], design=survey08_general.design[survey08_general$presgeneral_party=="Republican" | survey08_general$pid7>4], na.rm=T)
diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

col1<-paste(round(col1*100,0), "%",sep="")
col2<-paste(round(col2*100,0), "%",sep="")
col3<-paste(round(col3*100,0), "%",sep="")
col4<-paste(round(col4*100,0), "%",sep="")
col5<-paste(round(col5*100,0), "%",sep="")
col6<-paste(round(col6*100,0), "%",sep="")
diff_a<-paste(round(diff_a*100,0), "%",sep="")
diff_b<-paste(round(diff_b*100,0), "%",sep="")

xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "white"; fulltable <- rbind(fulltable,mat)

## discussed candidate
col1<-svymean(survey08_primary$discussed_candidate_oct[survey08_primary$primaryturnout=="Democrat"], design=survey08_primary.design[survey08_primary$primaryturnout=="Democrat"], na.rm=T)
col2<-svymean(survey08_general_noprim$discussed_candidate_oct[survey08_general_noprim$presgeneral_party=="Democrat"], design=survey08_general_noprim.design[survey08_general_noprim$presgeneral_party=="Democrat"], na.rm=T)
col3<-svymean(survey08_general$discussed_candidate_oct[survey08_general$presgeneral_party=="Democrat" | survey08_general$pid7<4], design=survey08_general.design[survey08_general$presgeneral_party=="Democrat" | survey08_general$pid7<4], na.rm=T)
diff<-col1-col2
z_test1 = (col1-col2)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col2)$SE^2)

col4<-svymean(survey08_primary$discussed_candidate_oct[survey08_primary$primaryturnout=="Republican"], design=survey08_primary.design[survey08_primary$primaryturnout=="Republican"], na.rm=T)
col5<-svymean(survey08_general_noprim$discussed_candidate_oct[survey08_general_noprim$presgeneral_party=="Republican"], design=survey08_general_noprim.design[survey08_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6<-svymean(survey08_general$discussed_candidate_oct[survey08_general$presgeneral_party=="Republican" | survey08_general$pid7>4], design=survey08_general.design[survey08_general$presgeneral_party=="Republican" | survey08_general$pid7>4], na.rm=T)
z_test2 = (col4-col5)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col5)$SE^2)

diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

col1<-paste(round(col1*100,0), "%",sep="")
col2<-paste(round(col2*100,0), "%",sep="")
col3<-paste(round(col3*100,0), "%",sep="")
col4<-paste(round(col4*100,0), "%",sep="")
col5<-paste(round(col5*100,0), "%",sep="")
col6<-paste(round(col6*100,0), "%",sep="")
diff_a<-paste(round(diff_a*100,0), "%",sep="")
diff_b<-paste(round(diff_b*100,0), "%",sep="")

xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "discussed candidate"; fulltable <- rbind(fulltable,mat)

## very interested in politics
col1<-svymean(survey08_primary$pol_interest[survey08_primary$primaryturnout=="Democrat"], design=survey08_primary.design[survey08_primary$primaryturnout=="Democrat"], na.rm=T)
col2<-svymean(survey08_general_noprim$pol_interest[survey08_general_noprim$presgeneral_party=="Democrat"], design=survey08_general_noprim.design[survey08_general_noprim$presgeneral_party=="Democrat"], na.rm=T)
col3<-svymean(survey08_general$pol_interest[survey08_general$presgeneral_party=="Democrat" | survey08_general$pid7<4], design=survey08_general.design[survey08_general$presgeneral_party=="Democrat" | survey08_general$pid7<4], na.rm=T)

col4<-svymean(survey08_primary$pol_interest[survey08_primary$primaryturnout=="Republican"], design=survey08_primary.design[survey08_primary$primaryturnout=="Republican"], na.rm=T)
col5<-svymean(survey08_general_noprim$pol_interest[survey08_general_noprim$presgeneral_party=="Republican"], design=survey08_general_noprim.design[survey08_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6<-svymean(survey08_general$pol_interest[survey08_general$presgeneral_party=="Republican" | survey08_general$pid7>4], design=survey08_general.design[survey08_general$presgeneral_party=="Republican" | survey08_general$pid7>4], na.rm=T)

diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

col1<-paste(round(col1*100,0), "%",sep="")
col2<-paste(round(col2*100,0), "%",sep="")
col3<-paste(round(col3*100,0), "%",sep="")
col4<-paste(round(col4*100,0), "%",sep="")
col5<-paste(round(col5*100,0), "%",sep="")
col6<-paste(round(col6*100,0), "%",sep="")
diff_a<-paste(round(diff_a*100,0), "%",sep="")
diff_b<-paste(round(diff_b*100,0), "%",sep="")

xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "very interested"; fulltable <- rbind(fulltable,mat)


## ideological self-placement
col1<-svymean(survey08_primary$self_ideology[survey08_primary$primaryturnout=="Democrat"], design=survey08_primary.design[survey08_primary$primaryturnout=="Democrat"], na.rm=T)
col2<-svymean(survey08_general_noprim$self_ideology[survey08_general_noprim$presgeneral_party=="Democrat"], design=survey08_general_noprim.design[survey08_general_noprim$presgeneral_party=="Democrat"], na.rm=T)
col3<-svymean(survey08_general$self_ideology[survey08_general$presgeneral_party=="Democrat" | survey08_general$pid7<4], design=survey08_general.design[survey08_general$presgeneral_party=="Democrat" | survey08_general$pid7<4], na.rm=T)

col4<-svymean(survey08_primary$self_ideology[survey08_primary$primaryturnout=="Republican"], design=survey08_primary.design[survey08_primary$primaryturnout=="Republican"], na.rm=T)
col5<-svymean(survey08_general_noprim$self_ideology[survey08_general_noprim$presgeneral_party=="Republican"], design=survey08_general_noprim.design[survey08_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6<-svymean(survey08_general$self_ideology[survey08_general$presgeneral_party=="Republican" | survey08_general$pid7>4], design=survey08_general.design[survey08_general$presgeneral_party=="Republican" | survey08_general$pid7>4], na.rm=T)

diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "self-placement"; fulltable <- rbind(fulltable,mat)


## CCAP idealpoint
col1<-svymean(survey08_primary$idealpoint[survey08_primary$primaryturnout=="Democrat" ], design=survey08_primary.design[survey08_primary$primaryturnout=="Democrat" ], na.rm=T)
col2<-svymean(survey08_general_noprim$idealpoint[survey08_general_noprim$presgeneral_party=="Democrat" ], design=survey08_general_noprim.design[survey08_general_noprim$presgeneral_party=="Democrat"  ], na.rm=T)
col3<-svymean(survey08_general$idealpoint[survey08_general$presgeneral_party=="Democrat" | survey08_general$pid7<4 ], design=survey08_general.design[survey08_general$presgeneral_party=="Democrat" | survey08_general$pid7<4 ], na.rm=T)

col4<-svymean(survey08_primary$idealpoint[survey08_primary$primaryturnout=="Republican" ], design=survey08_primary.design[survey08_primary$primaryturnout=="Republican" ], na.rm=T)
col5<-svymean(survey08_general_noprim$idealpoint[survey08_general_noprim$presgeneral_party=="Republican"  ], design=survey08_general_noprim.design[survey08_general_noprim$presgeneral_party=="Republican"  ], na.rm=T)
col6<-svymean(survey08_general$idealpoint[survey08_general$presgeneral_party=="Republican" | survey08_general$pid7>4 ], design=survey08_general.design[survey08_general$presgeneral_party=="Republican" | survey08_general$pid7>4 ], na.rm=T)

diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "idealpoint"; fulltable <- rbind(fulltable,mat)

## Raise taxes on incomes \$200K 
col1<-svymean(survey08_primary$bcap24_binary[survey08_primary$primaryturnout=="Democrat"], design=survey08_primary.design[survey08_primary$primaryturnout=="Democrat"], na.rm=T)
col2<-svymean(survey08_general_noprim$bcap24_binary[survey08_general_noprim$presgeneral_party=="Democrat"], design=survey08_general_noprim.design[survey08_general_noprim$presgeneral_party=="Democrat"], na.rm=T)
col3<-svymean(survey08_general$bcap24_binary[survey08_general$presgeneral_party=="Democrat" | survey08_general$pid7<4], design=survey08_general.design[survey08_general$presgeneral_party=="Democrat" | survey08_general$pid7<4], na.rm=T)

col4<-svymean(survey08_primary$bcap24_binary[survey08_primary$primaryturnout=="Republican"], design=survey08_primary.design[survey08_primary$primaryturnout=="Republican"], na.rm=T)
col5<-svymean(survey08_general_noprim$bcap24_binary[survey08_general_noprim$presgeneral_party=="Republican"], design=survey08_general_noprim.design[survey08_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6<-svymean(survey08_general$bcap24_binary[survey08_general$presgeneral_party=="Republican" | survey08_general$pid7>4], design=survey08_general.design[survey08_general$presgeneral_party=="Republican" | survey08_general$pid7>4], na.rm=T)

diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

col1<-paste(round(col1*100,0), "%",sep="")
col2<-paste(round(col2*100,0), "%",sep="")
col3<-paste(round(col3*100,0), "%",sep="")
col4<-paste(round(col4*100,0), "%",sep="")
col5<-paste(round(col5*100,0), "%",sep="")
col6<-paste(round(col6*100,0), "%",sep="")
diff_a<-paste(round(diff_a*100,0), "%",sep="")
diff_b<-paste(round(diff_b*100,0), "%",sep="")

xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "raise taxes"; fulltable <- rbind(fulltable,mat)


## Support civil unions
col1<-svymean(survey08_primary$bprofile19_binary[survey08_primary$primaryturnout=="Democrat"], design=survey08_primary.design[survey08_primary$primaryturnout=="Democrat"], na.rm=T)
col2<-svymean(survey08_general_noprim$bprofile19_binary[survey08_general_noprim$presgeneral_party=="Democrat"], design=survey08_general_noprim.design[survey08_general_noprim$presgeneral_party=="Democrat"], na.rm=T)
col3<-svymean(survey08_general$bprofile19_binary[survey08_general$presgeneral_party=="Democrat" | survey08_general$pid7<4], design=survey08_general.design[survey08_general$presgeneral_party=="Democrat" | survey08_general$pid7<4], na.rm=T)

col4<-svymean(survey08_primary$bprofile19_binary[survey08_primary$primaryturnout=="Republican"], design=survey08_primary.design[survey08_primary$primaryturnout=="Republican"], na.rm=T)
col5<-svymean(survey08_general_noprim$bprofile19_binary[survey08_general_noprim$presgeneral_party=="Republican"], design=survey08_general_noprim.design[survey08_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6<-svymean(survey08_general$bprofile19_binary[survey08_general$presgeneral_party=="Republican" | survey08_general$pid7>4], design=survey08_general.design[survey08_general$presgeneral_party=="Republican" | survey08_general$pid7>4], na.rm=T)

diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

col1<-paste(round(col1*100,0), "%",sep="")
col2<-paste(round(col2*100,0), "%",sep="")
col3<-paste(round(col3*100,0), "%",sep="")
col4<-paste(round(col4*100,0), "%",sep="")
col5<-paste(round(col5*100,0), "%",sep="")
col6<-paste(round(col6*100,0), "%",sep="")
diff_a<-paste(round(diff_a*100,0), "%",sep="")
diff_b<-paste(round(diff_b*100,0), "%",sep="")

xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "civil unions"; fulltable <- rbind(fulltable,mat)

fulltable
xtab <- print(xtable(fulltable))
cat(xtab,file="ccap2008.tex")

